package com.sisgr.controle;

import com.google.gson.Gson;
import com.sisgr.modelo.Colaborador;
import com.sisgr.modelo.servico.OAuthService;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Alessandro
 */
@Controller
public class ColaboradorController {
    
    //DI
    @Autowired
    private Colaborador colaborador;  
    @Autowired
    private OAuthService oas;
 
    private Gson gson;
    
    //Getters and Setters
    public Colaborador getColaborador() {
        return colaborador;
    }

    public void setColaborador(Colaborador colaborador) {
        this.colaborador = colaborador;
    }

    public OAuthService getOas() {
        return oas;
    }

    public void setOas(OAuthService oas) {
        this.oas = oas;
    }
    //fim dos getters and setters

    @RequestMapping("/home")
    public String home(Map<String, Object> model,
            @RequestParam("code") String code,
            @RequestParam("state") String state, HttpSession sessao) {

        if (code != null && state.equals("google")) {
            try {
               gson = new Gson();
               colaborador = gson.fromJson(oas.getUserInformationJson(code), Colaborador.class);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return "/colaborador/home";

        
    }
}
